<template>
  <div class="header">
    <!-- 折叠按钮 -->
    <el-menu
        class="menu"
        :default-active="onRoutes"
        mode="horizontal"
        router
        background-position: center
    >
      <div class="logo" >Flora植物养成社区</div>
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-submenu :index="item.index" :key="item.index">
            <template slot="title">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.menuName }}</span>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.menuName }}</span>
          </el-menu-item>
        </template>
      </template>
      <div class="header-right">
        <div class="header-user-con">
          <!-- 全屏显示 -->
          <div class="btn-fullscreen" @click="handleFullScreen">
            <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
              <i class="el-icon-rank"></i>
            </el-tooltip>
          </div>
          <!-- 用户头像 -->
          <div class="user-avator">
            <img v-if="user_avatar!=null&&user_avatar!=''" :src="user_avatar" />
<!--            <img src="../../assets/img/img.png" />-->
          </div>
          <!-- 用户名下拉菜单 -->
          <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{number}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item divided command="updatePwd">修改密码</el-dropdown-item>
              <el-dropdown-item divided command="loginout">{{login_or_logout}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </el-menu>
<!--      <div class="header-right">-->
<!--      <div class="header-user-con">-->
<!--        &lt;!&ndash; 全屏显示 &ndash;&gt;-->
<!--        <div class="btn-fullscreen" @click="handleFullScreen">-->
<!--          <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">-->
<!--            <i class="el-icon-rank"></i>-->
<!--          </el-tooltip>-->
<!--        </div>-->
<!--        &lt;!&ndash; 用户头像 &ndash;&gt;-->
<!--        <div class="user-avator">-->
<!--          &lt;!&ndash;                    <img v-if="headImg!=null&&headImg!=''" :src="headImg" />&ndash;&gt;-->
<!--          <img src="../../assets/img/img.png" />-->
<!--        </div>-->
<!--        &lt;!&ndash; 用户名下拉菜单 &ndash;&gt;-->
<!--        <el-dropdown class="user-name" trigger="click" @command="handleCommand">-->
<!--                    <span class="el-dropdown-link">-->
<!--                        {{number}}-->
<!--                        <i class="el-icon-caret-bottom"></i>-->
<!--                    </span>-->
<!--          <el-dropdown-menu slot="dropdown">-->
<!--            <el-dropdown-item divided command="updatePwd">修改密码</el-dropdown-item>-->
<!--            <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>-->
<!--          </el-dropdown-menu>-->
<!--        </el-dropdown>-->
<!--      </div>-->
<!--    </div>-->
  </div>
</template>

<script>
import {removeToken} from "@/utils/auth";
import {logout} from "@/api/user";
import bus from "@/components/common/bus";

export default {
  name: "Header",
  data() {
    return {
      user_avatar:'',
      collapse: false,
      fullscreen: false,
      number: '',
      //headImg: '',
      message: 2,
      login_or_logout:'请登录',
      hasLogin:false,
      items: [
        {
          icon: 'el-icon-s-home',
          index: 'MainPage',
          menuName: '首页'
        },
        {
          icon: 'el-icon-chat-line-round',
          index: 'Community',
          menuName: '社区'
        },
        {
          icon: 'el-icon-s-goods',
          index: 'Shop',
          menuName: '花市'
        },
        {
          icon: 'el-icon-key',
          index: 'My',
          menuName: '我的'
        }
      ],
      methods: {

      }
    };
  },
  created() {
    this.getLoginOrOut();
    this.getUserAvatar()
  },

  computed: {
    onRoutes() {
      return this.$route.path.replace('/', '');
    },
  },
  mounted() {

    // this上还有employeeId
    // console.log(this)
    // this.number = this.$store.getters.number
    // this.headImg = this.$store.getters.headImg
    // this.getLoginEmployee();
    // // this.$store.dispatch('getInfo')
    // if (document.body.clientWidth < 1500) {
    //   this.collapseChage();
    // }
  },
  methods: {
    routeChange(){
      this.getUserAvatar();
    },
    getLoginOrOut(){
      console.log(localStorage.getItem('userInfo'))
      if(localStorage.getItem('userInfo')){
        this.login_or_logout="退出登录";
      }else{
        this.login_or_logout="请登录";
      }
    },
    getUserAvatar(){
      console.log("shuhdiasc")
      let user=JSON.parse(localStorage.getItem("userInfo"))
      this.user_avatar=user.avatar

    },
    handleCommand(command) { // 用户名下拉菜单选择事件
      if (command == 'updatePwd') { // 修改密码
        //this.$router.push({path:'/updatePwd'})
      } else if (command == 'loginout') {
        if(this.login_or_logout=="退出登录"){
          var that = this;
          this.$confirm('是否确认退出?', '退出提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // console.log(that.$route.path);
            logout().then((response)=>{
              removeToken()
              localStorage.removeItem('userInfo');
              that.hasLogin = false;
              //window.location.reload();
              that.$message({
                type: 'success',
                message: '退出成功!'
              });
              this.$router.push({
                path: '/Login'
              });
            })
          }).catch(() => {
          });
        }else {
          removeToken()
          localStorage.removeItem('userInfo');
          this.hasLogin = false;
          this.$router.push({
            path: '/Login'
          });
        }
      }
    },
    // 侧边栏折叠
    collapseChage() {
      this.collapse = !this.collapse;
      bus.$emit('collapse', this.collapse);
    },
    // 全屏事件
    handleFullScreen() {
      let element = document.documentElement;
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      this.fullscreen = !this.fullscreen;
    },
    // ...mapActions("user/", ["setUserdata"])
  }
};
</script>

<style scoped>
.menu {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 60px;
  font-size: 22px;
  color: #fff;
  /*background-color: #242f42;*/
  background-color: #a0cba0;
}
/*.collapse-btn {*/
/*  float: left;*/
/*  padding: 0 21px;*/
/*  cursor: pointer;*/
/*  line-height: 70px;*/
/*}*/
.header .logo {
  float: left;
  width: 250px;
  line-height: 70px;
}
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  margin-left: 20px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
/*.el-dropdown-menu__item {*/
/*  text-align: center;*/
/*}*/
</style>
